package DBstep;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class iFileUpLoad2000 {
	public String FFilePath;
	public String FFileName;
	public byte[] FBuffer;
	public int FBufLen;
	public int FBinLen;
	public byte[] FSingBuf;
	public int FSingLen;
	public String FCopyright;

	public iFileUpLoad2000(HttpServletRequest request) {
		int totalRead = 0;
		int readBytes = 0;
		int mSingCur = 0;
		try {
			this.FBufLen = request.getContentLength();
			this.FBuffer = new byte[this.FBufLen];
			for (; totalRead < this.FBufLen; totalRead += readBytes) {
				request.getInputStream();
				readBytes = request.getInputStream().read(this.FBuffer, totalRead, this.FBufLen - totalRead);
			}

		} catch (Exception e) {
			System.out.println(e.toString());
		}
		for (this.FSingLen = 0; this.FBuffer[this.FSingLen] != 13; this.FSingLen += 1)
			;
		this.FSingBuf = new byte[this.FSingLen];
		for (mSingCur = 0; mSingCur < this.FSingLen; ++mSingCur)
			this.FSingBuf[mSingCur] = this.FBuffer[mSingCur];

		this.FFilePath = "";
		this.FFileName = "";
		this.FCopyright = "[数据驿站，版权所有]";
	}

	public int FindSing(int Start) {
		int result = -1;
		int s = Start;
		while (true) {
			if (s >= this.FBufLen - this.FSingLen) {
				result = -1;
				break;
			}
			int DjfOk = 1;
			int i = 0;
			while (true) {
				if (i >= this.FSingLen)
					break;
				if (this.FSingBuf[i] != this.FBuffer[(s + i)]) {
					DjfOk = 0;
					break;
				}
				++i;
			}
			if (DjfOk == 1) {
				result = s;
				break;
			}
			++s;
		}
		return result;
	}

	public int FindItem(String TagName) {
		int e;
		String strName;
		int result = -1;
		int s = 0;
//		label101:
		do {
			s = FindSing(s);
			if (s == -1)
//				break label101;
			s = s + this.FSingLen + 40;
			e = s;
			if (e + 1 >= this.FBufLen) {
				result = -1;
//				break label103;
			}
			while (this.FBuffer[(++e + 1)] != 34)
				;
			strName = new String(this.FBuffer, s, e - s + 1);
		} while (!(TagName.equalsIgnoreCase(strName)));

		result = e + 1;

		label101:

		label103: return result;
	}

	public String ItemValue(String TagName) {
		int e;
		String strTmp = "";
		String result = "";
		int s = FindItem(TagName);
		if (s == -1)
			return result;

		if (this.FBuffer[(s + 1)] == 59) {
			s += 13;
			e = s;
			while (this.FBuffer[(++e + 1)] != 34)
				;
		} else {
			s += 5;
			e = FindSing(s) - 3;
		}
		if (e - s < 0) {
			return result;
		}

		strTmp = new String(this.FBuffer, s, e - s + 1);
		result = strTmp;
		return result;
	}

	public String ExtName(String TagName) {
		String mFileName = "";
		String mExtName = "";
		mExtName = "";
		mFileName = ItemValue(TagName);
		if (mFileName != "")
			mExtName = mFileName.substring(mFileName.lastIndexOf("."));
		return mExtName;
	}

	public byte[] FileBody(String TagName) {
		byte[] mBody = (byte[]) null;
		int s = FindItem(TagName);
		if (s == -1)
			return mBody;
		s += 13;

		int i = s;
		while (this.FBuffer[(++i)] != 34)
			;
		int e = i - 1;
		if (e - s < 0)
			return mBody;
		i = e;
		while (true) {
			if ((this.FBuffer[(i - 4)] == 13) && (this.FBuffer[(i - 3)] == 10) && (this.FBuffer[(i - 2)] == 13) && (this.FBuffer[(i - 1)] == 10)) {
				s = i;
				break;
			}
			++i;
		}
		e = FindSing(s) - 3;
		if (e - s >= 0) {
			mBody = new byte[e - s + 1];
			int j = 0;
			for (i = s; i <= e; ++i) {
				mBody[j] = this.FBuffer[i];
				++j;
			}
		}

		return mBody;
	}

	public String FileName(String TagName) {
		String result = "";
		int s = FindItem(TagName);
		if (s == -1)
			return result;
		s += 13;

		int i = s;
		while (this.FBuffer[(++i)] != 34)
			;
		int e = i - 1;
		if (e - s < 0)
			return result;
		i = e;
		while (true) {
			if (i < s)
				break;
			if ((char) this.FBuffer[(i - 1)] == '\\') {
				s = i;
				break;
			}
			--i;
		}
		result = new String(this.FBuffer, s, e - s + 1);
		return result;
	}

	public int FileSize(String TagName) {
		int result = 0;
		int s = FindItem(TagName);
		if (s == -1)
			return result;
		s += 13;

		int i = s;
		while (this.FBuffer[(++i)] != 34)
			;
		int e = i - 1;
		if (e - s < 0)
			return result;
		i = e;
		while (true) {
			if ((this.FBuffer[(i - 4)] == 13) && (this.FBuffer[(i - 3)] == 10) && (this.FBuffer[(i - 2)] == 13) && (this.FBuffer[(i - 1)] == 10)) {
				s = i;
				break;
			}
			++i;
		}
		e = FindSing(s) - 3;
		result = e - s + 1;
		return result;
	}

	public String CopyRight() {
		return this.FCopyright;
	}

	public String getFilePath() {
		return this.FFilePath;
	}

	public String Request(String TagName) {
		return ItemValue(TagName);
	}

	public boolean SaveAll(String FileName) {
		boolean result = false;
		try {
			FileOutputStream mFile = new FileOutputStream(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(this.FFilePath)))
					.append('\\').append(FileName))));
			for (int i = 0; i < this.FBufLen; ++i)
				mFile.write(this.FBuffer[i]);

			mFile.close();
			result = true;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			result = false;
		}
		return result;
	}

	public boolean SaveFile(String TagName, String FileName) {
		String mFileName = "";
		boolean result = false;
		mFileName = FileName;
		int s = FindItem(TagName);
		if (s == -1) {
			result = false;
			return result;
		}
		s += 13;

		int i = s;
		while (this.FBuffer[(++i)] != 34)
			;
		int e = i - 1;
		if (e - s < 0) {
			result = false;
			return result;
		}
		i = e;
		while (true) {
			if (i < s)
				break;
			if ((char) this.FBuffer[(i - 1)] == '\\') {
				s = i;
				break;
			}
			--i;
		}
		this.FFileName = new String(this.FBuffer, s, e - s + 1);
		if (mFileName.trim() != "")
			this.FFileName = mFileName;
		i = e;
		while (true) {
			if ((this.FBuffer[(i - 4)] == 13) && (this.FBuffer[(i - 3)] == 10) && (this.FBuffer[(i - 2)] == 13) && (this.FBuffer[(i - 1)] == 10)) {
				s = i;
				break;
			}
			++i;
		}
		e = FindSing(s) - 3;
		try {
			FileOutputStream mFile = new FileOutputStream(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(this.FFilePath)))
					.append('\\').append(this.FFileName))));
			if (mFile != null) {
				if (e - s >= 0) {
					for (i = s; i <= e; ++i)
						mFile.write(this.FBuffer[i]);

					result = true;
				}
			} else
				result = false;

			mFile.close();
		} catch (Exception ex) {
			System.out.println(ex.toString());
			result = false;
		}
		return result;
	}

	public boolean setFilePath(String Value) {
		this.FFilePath = Value;
		File mFile = new File(this.FFilePath);
		mFile.mkdirs();
		return mFile.isDirectory();
	}
}